package com.sky.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    SetmealMapper setmealMapper;

    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    SetmealDishMapper setmealDishMapper;

    @Autowired
    DishMapper dishMapper;

    /**
     * 分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageSetmeal(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        List<Setmeal> list = setmealMapper.pageSetmeal(setmealPageQueryDTO);
        // 通过steam流给setmealVo赋值分类名和关联的菜品
        List<SetmealVO> voList = list.stream().map(setmeal -> {
            SetmealVO setmealVO = new SetmealVO();
            BeanUtils.copyProperties(setmeal, setmealVO);
            // 赋值分类关系
            Category category = categoryMapper.findByCategoryId(setmeal.getCategoryId());
            setmealVO.setCategoryName(category.getName());
            // 赋值关联菜品
            // List<SetmealDish> setmealDishList = setmealDishMapper.findSetmealDishBySetmealId(setmeal.getId());
            // setmealVO.setSetmealDishes(setmealDishList);
            return setmealVO;
        }).collect(Collectors.toList());
        PageInfo<Setmeal> pageInfo = new PageInfo<>(list);
        return new PageResult(pageInfo.getTotal(), voList);
    }

    /**
     * 添加套餐
     *
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void addSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        // 把setmealDTO中的套餐和菜品的关系拿出来添加到setmeal_dish表中
        setmealMapper.addSetmeal(setmeal);
        List<SetmealDish> list = setmealDTO.getSetmealDishes();
        List<SetmealDish> collect = list.stream().map(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());
            return setmealDish;
        }).collect(Collectors.toList());
        setmealDishMapper.addSetmealDish(collect);
    }


    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO findById(Long id) {
        Setmeal setmeal = setmealMapper.findById(id);
        List<SetmealDish> list = setmealDishMapper.findSetmealDishBySetmealId(setmeal.getId());
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(list);
        return setmealVO;
    }

    /**
     * 修改套餐
     *
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void updateSetmeal(SetmealDTO setmealDTO) {
        // 因为我们要判断修改的菜品中有没有停售的菜品,所以我们需要先进行菜品的修改操作,在执行修改套餐属性
        List<SetmealDish> setmealDishList = setmealDTO.getSetmealDishes();
        // 先判断是否启售，是启售在去执行这些判断,套餐是停售的不需要判断
        if (setmealDTO.getStatus() == StatusConstant.ENABLE) {
            setmealDishList.forEach(setmealDish -> {
                Dish dish = dishMapper.findById(setmealDish.getDishId());
                if (dish.getStatus() == StatusConstant.DISABLE) {
                    throw new SetmealEnableFailedException(MessageConstant.DISH_IS_HALT_SALES + ":" + dish.getName());
                }
            });
        }
        // 如果菜品状态没有问题,不管传进来的菜品有没有都先执行执行删除
        setmealDishMapper.delBySetmealId(setmealDTO.getId());
        // 然后在判断传进来的是否为空,如果为空,我们就不需要在做添加操作了
        if (setmealDishList.size() == 0) {
            return;
        }
        // 添加
        List<SetmealDish> collect = setmealDishList.stream().map(setmealDish -> {
            setmealDish.setSetmealId(setmealDTO.getId());
            return setmealDish;
        }).collect(Collectors.toList());
        setmealDishMapper.addSetmealDish(collect);

        // 修改好菜品关系之后,我们就可以放心的修改套餐属性了
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.updateSetmeal(setmeal);
    }


    /**
     * 删除套餐
     *
     * @param ids
     */
    @Transactional
    @Override
    public void delByIds(List<Long> ids) {
        // 没选择不能删除
        if (ids.size() == 0) {
            throw new DeletionNotAllowedException(MessageConstant.UNDEFINED_DELETE);
        }
        // 状态为启售的不能删除
        ids.forEach(id -> {
            Setmeal setmeal = setmealMapper.findById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });
        // 删除套餐
        setmealMapper.delByIds(ids);
        // 删除套餐相关的菜品
        ids.forEach(id -> {
            setmealDishMapper.delBySetmealId(id);
        });
    }


    /**
     * 启售和停售套餐
     *
     * @param status
     * @param id
     */
    @Transactional
    @Override
    public void updateStatus(Integer status, Long id) {
        // 如果是开启在进行判断,停售就不进行判断
        if (status == StatusConstant.ENABLE) {
            // 启售套餐之前,判断一下有没有未启售的菜品
            List<SetmealDish> list = setmealDishMapper.findSetmealDishBySetmealId(id);
            list.forEach(setmealDish -> {
                Dish dish = dishMapper.findById(setmealDish.getDishId());
                if (dish.getStatus().equals(StatusConstant.DISABLE)) {
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            });
        }
        Setmeal setmeal = Setmeal.builder().status(status).id(id).build();
        setmealMapper.updateSetmeal(setmeal);
    }

    @Override
    public List<Setmeal> list(SetmealDTO setmealDTO) {
        List<Setmeal> list = setmealMapper.list(setmealDTO);
        return list;
    }



    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        //根据套餐id查询绑定的所有菜品关系
        List<SetmealDish> setmealDishes = setmealDishMapper.findSetmealDishBySetmealId(id);

        List<DishItemVO> dishItemList = setmealDishes.stream()
                .map(setmealDish -> {
                    Long dishId = setmealDish.getDishId();
                    //根据菜品id查询菜品信息
                    Dish dish = dishMapper.findById(dishId);
                    //封装DishItem
                    DishItemVO dishItemVO = new DishItemVO();
                    dishItemVO.setImage(dish.getImage());
                    dishItemVO.setDescription(dish.getDescription());
                    dishItemVO.setCopies(setmealDish.getCopies());
                    dishItemVO.setName(setmealDish.getName());
                    return dishItemVO;
                }).collect(Collectors.toList());

        return dishItemList;
    }
}
